/*
 * OhtuView.java
 */
package ohtu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;

/**
 * The application's main frame.
 */
public class OhtuView extends FrameView {

	public OhtuView(SingleFrameApplication app) {
		super(app);

		initController();
		initComponents();

		getFrame().setTitle("Kalenteriohjelma");
		updateAll();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                mainTabbedPanel = new javax.swing.JTabbedPane();
                schedulePanel = new javax.swing.JPanel();
                scheduleScrollPane = new javax.swing.JScrollPane();
                scheduleTable = new javax.swing.JTable();
                nextWeekButton = new javax.swing.JButton();
                previousWeekButton = new javax.swing.JButton();
                addEventButton = new javax.swing.JButton();
                editEventButton = new javax.swing.JButton();
                eventInfoPane = new javax.swing.JScrollPane();
                eventInfoArea = new javax.swing.JTextArea();
                removeEventButton = new javax.swing.JButton();
                browsedDate = new javax.swing.JLabel();
                currentCoursesPanel = new javax.swing.JPanel();
                courseScrollPane = new javax.swing.JScrollPane();
                courseList = new javax.swing.JList();
                addCourseButton = new javax.swing.JButton();
                removeCourseButton = new javax.swing.JButton();
                gradeCourseButton = new javax.swing.JButton();
                editCourseButton = new javax.swing.JButton();
                addCourseEventButton = new javax.swing.JButton();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                editCourseEventButton = new javax.swing.JButton();
                removeCourseEventButton = new javax.swing.JButton();
                courseEventScrollPane = new javax.swing.JScrollPane();
                courseEventList = new javax.swing.JList();
                gradePanel = new javax.swing.JPanel();
                gradeScrollPane = new javax.swing.JScrollPane();
                gradeTable = new javax.swing.JTable();
                removeGradeButton = new javax.swing.JButton();
                editGradeButton = new javax.swing.JButton();
                totalPointsLabel = new javax.swing.JLabel();
                averageGradeLabel = new javax.swing.JLabel();
                jTextField1 = new javax.swing.JTextField();

                mainTabbedPanel.setName("mainTabbedPanel"); // NOI18N

                schedulePanel.setName("schedulePanel"); // NOI18N

                scheduleScrollPane.setName("scheduleScrollPane"); // NOI18N

                scheduleTable.setModel(new javax.swing.table.DefaultTableModel(
                        new Object [][] {
                                {"8-10", null, null, null, null, null, null, null},
                                {"10-12", "Ruotsi", null, "Ohjelmoinnin jatkokurssi", null, null, null, null},
                                {"12-14", "Ruotsi", null, "Ohjelmoinnin jatkokurssi", null, null, null, null},
                                {"14-16", null, null, null, null, "Sulkapallo", null, null},
                                {"16-18", null, null, null, null, null, null, null},
                                {"18-20", null, null, null, null, null, null, null},
                                {"20-22", null, null, null, null, null, null, null}
                        },
                        new String [] {
                                "Aika", "Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"
                        }
                ) {
                        Class[] types = new Class [] {
                                java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
                        };
                        boolean[] canEdit = new boolean [] {
                                false, false, false, false, false, false, false, false
                        };

                        public Class getColumnClass(int columnIndex) {
                                return types [columnIndex];
                        }

                        public boolean isCellEditable(int rowIndex, int columnIndex) {
                                return canEdit [columnIndex];
                        }
                });
                scheduleTable.setCellSelectionEnabled(true);
                scheduleTable.setName("scheduleTable"); // NOI18N
                scheduleTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
                scheduleTable.getTableHeader().setReorderingAllowed(false);
                scheduleTable.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mouseClicked(java.awt.event.MouseEvent evt) {
                                scheduleTableMouseClicked(evt);
                        }
                });
                scheduleScrollPane.setViewportView(scheduleTable);
                org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ohtu.OhtuApp.class).getContext().getResourceMap(OhtuView.class);
                scheduleTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                scheduleTable.getColumnModel().getColumn(0).setResizable(false);
                scheduleTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title0")); // NOI18N
                scheduleTable.getColumnModel().getColumn(1).setResizable(false);
                scheduleTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title1")); // NOI18N
                scheduleTable.getColumnModel().getColumn(2).setResizable(false);
                scheduleTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title2")); // NOI18N
                scheduleTable.getColumnModel().getColumn(3).setResizable(false);
                scheduleTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title3")); // NOI18N
                scheduleTable.getColumnModel().getColumn(4).setResizable(false);
                scheduleTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title4")); // NOI18N
                scheduleTable.getColumnModel().getColumn(5).setResizable(false);
                scheduleTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title5")); // NOI18N
                scheduleTable.getColumnModel().getColumn(6).setResizable(false);
                scheduleTable.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title6")); // NOI18N
                scheduleTable.getColumnModel().getColumn(7).setResizable(false);
                scheduleTable.getColumnModel().getColumn(7).setHeaderValue(resourceMap.getString("scheduleTable.columnModel.title7")); // NOI18N

                nextWeekButton.setText(resourceMap.getString("nextWeekButton.text")); // NOI18N
                nextWeekButton.setName("nextWeekButton"); // NOI18N
                nextWeekButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                nextWeekButtonActionPerformed(evt);
                        }
                });

                previousWeekButton.setText(resourceMap.getString("previousWeekButton.text")); // NOI18N
                previousWeekButton.setName("previousWeekButton"); // NOI18N
                previousWeekButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                previousWeekButtonActionPerformed(evt);
                        }
                });

                addEventButton.setText(resourceMap.getString("addEventButton.text")); // NOI18N
                addEventButton.setName("addEventButton"); // NOI18N
                addEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                addEventButtonActionPerformed(evt);
                        }
                });

                editEventButton.setText(resourceMap.getString("editEventButton.text")); // NOI18N
                editEventButton.setName("editEventButton"); // NOI18N
                editEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                editEventButtonActionPerformed(evt);
                        }
                });

                eventInfoPane.setName("eventInfoPane"); // NOI18N

                eventInfoArea.setColumns(20);
                eventInfoArea.setEditable(false);
                eventInfoArea.setRows(5);
                eventInfoArea.setText(resourceMap.getString("eventInfoArea.text")); // NOI18N
                eventInfoArea.setName("eventInfoArea"); // NOI18N
                eventInfoPane.setViewportView(eventInfoArea);

                removeEventButton.setText(resourceMap.getString("removeEventButton.text")); // NOI18N
                removeEventButton.setName("removeEventButton"); // NOI18N
                removeEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                removeEventButtonActionPerformed(evt);
                        }
                });

                browsedDate.setFont(resourceMap.getFont("browsedDate.font")); // NOI18N
                browsedDate.setText(resourceMap.getString("browsedDate.text")); // NOI18N
                browsedDate.setName("browsedDate"); // NOI18N

                javax.swing.GroupLayout schedulePanelLayout = new javax.swing.GroupLayout(schedulePanel);
                schedulePanel.setLayout(schedulePanelLayout);
                schedulePanelLayout.setHorizontalGroup(
                        schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(schedulePanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(scheduleScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 832, Short.MAX_VALUE)
                                        .addComponent(browsedDate, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(schedulePanelLayout.createSequentialGroup()
                                                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(addEventButton)
                                                        .addComponent(removeEventButton)
                                                        .addComponent(editEventButton))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(schedulePanelLayout.createSequentialGroup()
                                                                .addGap(426, 426, 426)
                                                                .addComponent(previousWeekButton)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(nextWeekButton))
                                                        .addComponent(eventInfoPane, javax.swing.GroupLayout.DEFAULT_SIZE, 722, Short.MAX_VALUE))))
                                .addContainerGap())
                );

                schedulePanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {nextWeekButton, previousWeekButton});

                schedulePanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addEventButton, editEventButton, removeEventButton});

                schedulePanelLayout.setVerticalGroup(
                        schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(schedulePanelLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(browsedDate, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(scheduleScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(schedulePanelLayout.createSequentialGroup()
                                                .addComponent(addEventButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(removeEventButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(editEventButton))
                                        .addComponent(eventInfoPane, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(schedulePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(nextWeekButton)
                                        .addComponent(previousWeekButton))
                                .addContainerGap())
                );

                mainTabbedPanel.addTab(resourceMap.getString("schedulePanel.TabConstraints.tabTitle"), schedulePanel); // NOI18N

                currentCoursesPanel.setName("currentCoursesPanel"); // NOI18N
                currentCoursesPanel.setPreferredSize(new java.awt.Dimension(682, 403));

                courseScrollPane.setName("courseScrollPane"); // NOI18N

                courseList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
                courseList.setName("courseList"); // NOI18N
                courseList.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mouseClicked(java.awt.event.MouseEvent evt) {
                                courseListMouseClicked(evt);
                        }
                });
                courseScrollPane.setViewportView(courseList);

                addCourseButton.setText(resourceMap.getString("addCourseButton.text")); // NOI18N
                addCourseButton.setName("addCourseButton"); // NOI18N
                addCourseButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                addCourseButtonActionPerformed(evt);
                        }
                });

                removeCourseButton.setText(resourceMap.getString("removeCourseButton.text")); // NOI18N
                removeCourseButton.setName("removeCourseButton"); // NOI18N
                removeCourseButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                removeCourseButtonActionPerformed(evt);
                        }
                });

                gradeCourseButton.setText(resourceMap.getString("gradeCourseButton.text")); // NOI18N
                gradeCourseButton.setName("gradeCourseButton"); // NOI18N
                gradeCourseButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                gradeCourseButtonActionPerformed(evt);
                        }
                });

                editCourseButton.setText(resourceMap.getString("editCourseButton.text")); // NOI18N
                editCourseButton.setName("editCourseButton"); // NOI18N
                editCourseButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                editCourseButtonActionPerformed(evt);
                        }
                });

                addCourseEventButton.setText(resourceMap.getString("addCourseEventButton.text")); // NOI18N
                addCourseEventButton.setName("addCourseEventButton"); // NOI18N
                addCourseEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                addCourseEventButtonActionPerformed(evt);
                        }
                });

                jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
                jLabel1.setName("jLabel1"); // NOI18N

                jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
                jLabel2.setName("jLabel2"); // NOI18N

                editCourseEventButton.setText(resourceMap.getString("editCourseEventButton.text")); // NOI18N
                editCourseEventButton.setName("editCourseEventButton"); // NOI18N
                editCourseEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                editCourseEventButtonActionPerformed(evt);
                        }
                });

                removeCourseEventButton.setText(resourceMap.getString("removeCourseEventButton.text")); // NOI18N
                removeCourseEventButton.setName("removeCourseEventButton"); // NOI18N
                removeCourseEventButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                removeCourseEventButtonActionPerformed(evt);
                        }
                });

                courseEventScrollPane.setName("courseEventScrollPane"); // NOI18N

                courseEventList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
                courseEventList.setName("courseEventList"); // NOI18N
                courseEventScrollPane.setViewportView(courseEventList);

                javax.swing.GroupLayout currentCoursesPanelLayout = new javax.swing.GroupLayout(currentCoursesPanel);
                currentCoursesPanel.setLayout(currentCoursesPanelLayout);
                currentCoursesPanelLayout.setHorizontalGroup(
                        currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(currentCoursesPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(removeCourseButton)
                                        .addComponent(addCourseButton)
                                        .addComponent(editCourseButton)
                                        .addComponent(gradeCourseButton))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(courseScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1))
                                .addGap(12, 12, 12)
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(courseEventScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(addCourseEventButton)
                                        .addComponent(editCourseEventButton)
                                        .addComponent(removeCourseEventButton))
                                .addContainerGap())
                );

                currentCoursesPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addCourseButton, editCourseButton, gradeCourseButton, removeCourseButton});

                currentCoursesPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addCourseEventButton, editCourseEventButton, removeCourseEventButton});

                currentCoursesPanelLayout.setVerticalGroup(
                        currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(currentCoursesPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1))
                                .addGap(6, 6, 6)
                                .addGroup(currentCoursesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(courseEventScrollPane)
                                        .addGroup(currentCoursesPanelLayout.createSequentialGroup()
                                                .addComponent(addCourseEventButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(editCourseEventButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(removeCourseEventButton))
                                        .addGroup(currentCoursesPanelLayout.createSequentialGroup()
                                                .addComponent(addCourseButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(removeCourseButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(editCourseButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(gradeCourseButton))
                                        .addComponent(courseScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE))
                                .addContainerGap(109, Short.MAX_VALUE))
                );

                mainTabbedPanel.addTab(resourceMap.getString("currentCoursesPanel.TabConstraints.tabTitle"), currentCoursesPanel); // NOI18N

                gradePanel.setName("gradePanel"); // NOI18N

                gradeScrollPane.setName("gradeScrollPane"); // NOI18N

                gradeTable.setModel(new javax.swing.table.DefaultTableModel(
                        new Object [][] {
                                {"", "", ""},
                                {"", "", ""},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null},
                                {null, null, null}
                        },
                        new String [] {
                                "Kurssi", "Pisteet", "Arvosana"
                        }
                ) {
                        boolean[] canEdit = new boolean [] {
                                false, false, false
                        };

                        public boolean isCellEditable(int rowIndex, int columnIndex) {
                                return canEdit [columnIndex];
                        }
                });
                gradeTable.setName("gradeTable"); // NOI18N
                gradeTable.getTableHeader().setReorderingAllowed(false);
                gradeScrollPane.setViewportView(gradeTable);
                gradeTable.getColumnModel().getColumn(0).setResizable(false);
                gradeTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("gradeTable.columnModel.title0")); // NOI18N
                gradeTable.getColumnModel().getColumn(1).setResizable(false);
                gradeTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("gradeTable.columnModel.title1")); // NOI18N
                gradeTable.getColumnModel().getColumn(2).setResizable(false);
                gradeTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("gradeTable.columnModel.title2")); // NOI18N

                removeGradeButton.setText(resourceMap.getString("removeGradeButton.text")); // NOI18N
                removeGradeButton.setName("removeGradeButton"); // NOI18N
                removeGradeButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                removeGradeButtonActionPerformed(evt);
                        }
                });

                editGradeButton.setText(resourceMap.getString("editGradeButton.text")); // NOI18N
                editGradeButton.setName("editGradeButton"); // NOI18N
                editGradeButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                editGradeButtonActionPerformed(evt);
                        }
                });

                totalPointsLabel.setText(resourceMap.getString("totalPointsLabel.text")); // NOI18N
                totalPointsLabel.setName("totalPointsLabel"); // NOI18N

                averageGradeLabel.setText(resourceMap.getString("averageGradeLabel.text")); // NOI18N
                averageGradeLabel.setName("averageGradeLabel"); // NOI18N

                javax.swing.GroupLayout gradePanelLayout = new javax.swing.GroupLayout(gradePanel);
                gradePanel.setLayout(gradePanelLayout);
                gradePanelLayout.setHorizontalGroup(
                        gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(gradePanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(gradeScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 832, Short.MAX_VALUE)
                                        .addGroup(gradePanelLayout.createSequentialGroup()
                                                .addGroup(gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(removeGradeButton)
                                                        .addComponent(editGradeButton))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(averageGradeLabel)
                                                        .addComponent(totalPointsLabel))))
                                .addContainerGap())
                );

                gradePanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {editGradeButton, removeGradeButton});

                gradePanelLayout.setVerticalGroup(
                        gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gradePanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(gradeScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addGroup(gradePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(gradePanelLayout.createSequentialGroup()
                                                .addComponent(removeGradeButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(editGradeButton))
                                        .addGroup(gradePanelLayout.createSequentialGroup()
                                                .addComponent(totalPointsLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(averageGradeLabel)))
                                .addGap(141, 141, 141))
                );

                mainTabbedPanel.addTab(resourceMap.getString("gradePanel.TabConstraints.tabTitle"), gradePanel); // NOI18N

                jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
                jTextField1.setName("jTextField1"); // NOI18N

                setComponent(mainTabbedPanel);
        }// </editor-fold>//GEN-END:initComponents

	private void updateAll() {
		updateCourses();
		updateDate();
		updateSchedule();
		updateTotalPointsLabel();
		updateAverageGradeLabel();
		updateGradeTable();

		ObjectIOHandler.save(c, "data.dat");
	}

	private void updateCourses() {
		courseList.setListData(c.getCourses().toArray());
	}

	private void updateEvents() {
		Course course = (Course) courseList.getSelectedValue();
		if (course != null) {
			courseEventList.setListData(course.getEvents().getAllEvents().toArray());
		} else {
			courseEventList.setListData(new Object[0]);
		}
	}

	private void updateDate() {
		browsedDate.setText("Viikko " + c.getShownWeek().get(Calendar.WEEK_OF_YEAR) + " | " + dateFormat.format(c.getShownWeek().getTime()) + " - " + dateFormat.format(c.getNextWeek().getTime()));
	}

	private void updateSchedule() {
		c.clearScheduleTable(scheduleTable);
		c.drawEventsToTable(scheduleTable);
	}

	private void updateGradeTable() {
		for (int i = 0; i < gradeTable.getRowCount(); ++i) {
			for (int j = 0; j < 3; ++j) {
				gradeTable.getModel().setValueAt(null, i, j);
			}
		}
		for (Course course : c.getCompletedCourses()) {
			gradeTable.getModel().setValueAt(course, c.getCompletedCourses().indexOf(course), 0);
		}
		for (Course course : c.getCompletedCourses()) {
			gradeTable.getModel().setValueAt(course.getPoints(), c.getCompletedCourses().indexOf(course), 1);
		}
		for (Course course : c.getCompletedCourses()) {
			gradeTable.getModel().setValueAt(course.getGrade(), c.getCompletedCourses().indexOf(course), 2);
		}
	}

    private void nextWeekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextWeekButtonActionPerformed
	    c.nextWeek();
	    updateAll();
    }//GEN-LAST:event_nextWeekButtonActionPerformed

    private void previousWeekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousWeekButtonActionPerformed
	    c.previousWeek();
	    updateAll();
    }//GEN-LAST:event_previousWeekButtonActionPerformed

    private void addEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addEventButtonActionPerformed
	    new AddEventDialog(this.getFrame(), c, null, Calendar.getInstance(), null);
	    updateAll();
    }//GEN-LAST:event_addEventButtonActionPerformed

    private void gradeCourseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gradeCourseButtonActionPerformed
	    new GradeCourseDialog(this.getFrame(), (Course) courseList.getSelectedValue(), c);
	    updateAll();
    }//GEN-LAST:event_gradeCourseButtonActionPerformed

    private void addCourseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCourseButtonActionPerformed
	    new AddCourseDialog(this.getFrame(), c.getCourses(), null);
	    updateAll();
    }//GEN-LAST:event_addCourseButtonActionPerformed

    private void addCourseEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCourseEventButtonActionPerformed
	    Course course = (Course) courseList.getSelectedValue();
	    if (course != null) {
		    new AddEventDialog(this.getFrame(), c, course, Calendar.getInstance(), null);
	    }
	    updateEvents();
    }//GEN-LAST:event_addCourseEventButtonActionPerformed

	private void removeGradeButtonActionPerformed(java.awt.event.ActionEvent evt) {
		c.getCompletedCourses().remove(gradeTable.getSelectedRow());
		updateAll();
	}

	private void editGradeButtonActionPerformed(java.awt.event.ActionEvent evt) {
		new GradeCourseDialog(this.getFrame(), (Course) gradeTable.getModel().getValueAt(gradeTable.getSelectedRow(), 0), c);
		updateAll();
	}

	private void updateTotalPointsLabel() {
		totalPointsLabel.setText("Pisteitä yhteensä: " + Integer.toString(c.getTotalPoints()));
	}

	private void updateAverageGradeLabel() {
		averageGradeLabel.setText("Keskiarvo: " + Double.toString(c.getAverageGrade()));
	}

    private void scheduleTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scheduleTableMouseClicked
	    if (scheduleTable.getSelectedColumn() < 1) {
		    return;
	    }
	    Event selectedEvent = (Event) scheduleTable.getModel().getValueAt(scheduleTable.getSelectedRow(), scheduleTable.getSelectedColumn());
	    if (selectedEvent == null) {
		    eventInfoArea.setText("");
	    } else {
		    eventInfoArea.setText(selectedEvent.getInfo());
	    }
    }//GEN-LAST:event_scheduleTableMouseClicked

    private void editCourseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCourseButtonActionPerformed
	    Course selectedCourse = (Course) courseList.getSelectedValue();
	    if (selectedCourse == null) {
		    return;
	    }
	    new AddCourseDialog(this.getFrame(), c.getCourses(), selectedCourse);
	    updateAll();
    }//GEN-LAST:event_editCourseButtonActionPerformed

    private void removeEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeEventButtonActionPerformed
	    c.removeEventFromContainer(c.getSelectedEvent(scheduleTable));
	    updateAll();
    }//GEN-LAST:event_removeEventButtonActionPerformed

    private void removeCourseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeCourseButtonActionPerformed
	    c.removeCourse((Course) courseList.getSelectedValue());
	    updateAll();
    }//GEN-LAST:event_removeCourseButtonActionPerformed

    private void removeCourseEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeCourseEventButtonActionPerformed
	    Course course = (Course) courseList.getSelectedValue();
	    Event event = (Event) courseEventList.getSelectedValue();
	    if (course != null && event != null) {
		    course.remove(event);
		    c.getEventContainer().remove(event);
	    }
	    updateEvents();
	    updateAll();
    }//GEN-LAST:event_removeCourseEventButtonActionPerformed

    private void editCourseEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCourseEventButtonActionPerformed
	    Course course = (Course) courseList.getSelectedValue();
	    Event event = (Event) courseEventList.getSelectedValue();
	    if (course != null && event != null) {
		    new AddEventDialog(this.getFrame(), c, course, Calendar.getInstance(), event);
	    }
	    updateAll();
    }//GEN-LAST:event_editCourseEventButtonActionPerformed

    private void courseListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_courseListMouseClicked
	    Course course = (Course) courseList.getSelectedValue();
	    if (course != null) {
		    updateEvents();
	    }
    }//GEN-LAST:event_courseListMouseClicked

    private void editEventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editEventButtonActionPerformed
	    Event selectedEvent = c.getSelectedEvent(scheduleTable);
	    new AddEventDialog(this.getFrame(), c, selectedEvent.getCourse(), selectedEvent.getStartDate(), selectedEvent);
	    updateAll();
    }//GEN-LAST:event_editEventButtonActionPerformed

        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton addCourseButton;
        private javax.swing.JButton addCourseEventButton;
        private javax.swing.JButton addEventButton;
        private javax.swing.JLabel averageGradeLabel;
        private javax.swing.JLabel browsedDate;
        private javax.swing.JList courseEventList;
        private javax.swing.JScrollPane courseEventScrollPane;
        private javax.swing.JList courseList;
        private javax.swing.JScrollPane courseScrollPane;
        private javax.swing.JPanel currentCoursesPanel;
        private javax.swing.JButton editCourseButton;
        private javax.swing.JButton editCourseEventButton;
        private javax.swing.JButton editEventButton;
        private javax.swing.JButton editGradeButton;
        private javax.swing.JTextArea eventInfoArea;
        private javax.swing.JScrollPane eventInfoPane;
        private javax.swing.JButton gradeCourseButton;
        private javax.swing.JPanel gradePanel;
        private javax.swing.JScrollPane gradeScrollPane;
        private javax.swing.JTable gradeTable;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTabbedPane mainTabbedPanel;
        private javax.swing.JButton nextWeekButton;
        private javax.swing.JButton previousWeekButton;
        private javax.swing.JButton removeCourseButton;
        private javax.swing.JButton removeCourseEventButton;
        private javax.swing.JButton removeEventButton;
        private javax.swing.JButton removeGradeButton;
        private javax.swing.JPanel schedulePanel;
        private javax.swing.JScrollPane scheduleScrollPane;
        private javax.swing.JTable scheduleTable;
        private javax.swing.JLabel totalPointsLabel;
        // End of variables declaration//GEN-END:variables
	static SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
	static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
	Controller c;

	private void initController() {
		if (!ObjectIOHandler.exists("data.dat")) {
			c = new Controller();
			ObjectIOHandler.save(c, "data.dat");
		} else {
			c = (Controller) ObjectIOHandler.load("data.dat");
		}
	}
}
